import { ItagRoute } from "@renderer/interface/tag";
import { defineStore } from "pinia";

// 定义一个名为 tag 的仓库
export const useTagStore = defineStore('tag', {
	state: () => ({
		viewTags: [] as ItagRoute[]
	}),
	getters: {
		// 用户缓存组件的组件名称数组
		aliveName: state => state.viewTags.map(item => item.name as string)
	},
	actions: {
		// 添加标签的方法
		pushViewTags(route: ItagRoute) {
			!this.viewTags.find(item => item.path === route.path) && this.viewTags.push(route)
		},
		// 删除标签的方法
		rmViewTag(route: ItagRoute) {
			this.viewTags.forEach((item, index) => {
				if (item.path === route.path) {
					this.viewTags.splice(index, 1)
				}
			})
		}
	},
	persist: {
		enabled: true,
		strategies: [
			{
				storage: localStorage,
				paths: ['viewTags']
			}
		]
	}
})